package com.java.service;

import com.java.dao.*;
import com.java.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class EnterGoodServiceImpl implements EnterGoodService {
    @Autowired
    private EnterGoodMapper enterGoodMapper;
    @Autowired
    private EnterGoodOkMapper enterGoodOkMapper;
    @Autowired
    private IntoAndOutMapper intoAndOutMapper;
    @Autowired
    private EnterGoodsMapper enterGoodsMapper;
    @Autowired
    private BankMapper bankMapper;


    @Override
    public Page<EnterGood> getPageByPage(Integer a, String document) {
        int b=4;//显示条数
        Page<EnterGood> page=new Page<>();
        page.setCurryPage(a);//设置当前页码
        page.setRows(b);//设置每页显示的条数
        Integer count = enterGoodMapper.getCount(document);//总条数
        page.setTotalCount(count);//总条数
        int start=(a-1)*b;/*开始的索引*/
        List<EnterGood> list = enterGoodMapper.getListEnterGood(start, b, document);
        page.setList(list);/*赋值list*/
        int totalPae=count % b==0 ? count / b : (count / b)+1;
        page.setTotalPae(totalPae);/*设置总页数*/
        if(page==null){
            page=null;
        }
        return page;
    }

    @Override
    public Integer getCount(String document) {
        return enterGoodMapper.getCount(document);
    }


    /*添加操作*/
    @Override
    public int addEnterGood(EnterGood enterGood) {
        EnterGood good = enterGoodMapper.getEnterGoodByDocument(enterGood.getDocument());
        if(good==null){
        return enterGoodMapper.addEnterGood(enterGood);
        }
        return 0;
    }

    @Override
    public int deleteEnterGood(String eid) {
        return enterGoodMapper.deleteEnterGood(eid);
    }

    @Override
    public EnterGood getEnterGoodByEGid(Integer egid) {
        return enterGoodMapper.getEnterGoodByEGid(egid);
    }

    /**/
    @Override
    public int updateEnterGoodByEnterGoodsByDel(EnterGood enterGood) {
        return enterGoodMapper.updateEnterGoodByEnterGoodsByDel(enterGood);
    }

    @Override
    public EnterGood test(Integer eid) {
        return enterGoodMapper.getEnterGoodAndEnterGoodsListByEid(eid);
    }


    /*入库操作*/
    @Override
    public boolean update(Integer eid) {
        /*根据主外键关联查出一对多的entergood和entergoods*/
        EnterGood good = enterGoodMapper.getEnterGoodBYEid(eid);

        //创建bank对象
        Bank bank=new Bank();
        bank.setGoods2(good.getGoodname());
        bank.setCountmoney2(good.getMoney());




        /*创建入库/出库对象并且赋值*/
        IntoAndOut intoAndOut=new IntoAndOut();
        intoAndOut.setEgid(eid);//外键
        intoAndOut.setDocumentid(good.getDocument());//编号
        intoAndOut.setData1(good.getData1());//时间
        intoAndOut.setStores(good.getStores());//门店
        intoAndOut.setRid(good.getRid());//仓库
        intoAndOut.setFrom1(good.getForname());//供应商
        intoAndOut.setGood(good.getGoodname());//商品名称
        intoAndOut.setGoodstyle(good.getGoodStyle());//商品类型
        intoAndOut.setNum(good.getNum());//数量

        int add2=0;
        int add3=0;
        /*创建进货成功对象使用EnterGoodOk并且赋值*/
        List<EnterGoods> list = enterGoodsMapper.getEnterGoodsListByEgid(eid);
        list.forEach(System.out::println);

        /*判断该外键是否存在.存在不需要添加 直接修改即可*/
        List<EnterGoodOk> ok = enterGoodOkMapper.getEnterGoodOkByEgid(eid);
        System.out.println("ok:"+ok+"eid:"+eid);
        System.out.println("list:----"+list);
        if(ok.size()==0){
            for (int i = 0; i < list.size(); i++) {
                /*添加:如果外键不存在.那么查询订单号是否存在,不存在则直接添加*/
                EnterGoodOk enterGoodOkBydocument2 = enterGoodOkMapper.getEnterGoodOkBydocument2(list.get(i).getGoodname2());
                System.out.println(enterGoodOkBydocument2 + "000000000000000000000");
                if (enterGoodOkBydocument2 == null) {
                    EnterGoodOk enterGoodOk = new EnterGoodOk();
                    enterGoodOk.setEgid(eid);//外键
                    enterGoodOk.setDocument(good.getDocument());//EnterGood的编号
                    enterGoodOk.setDocument2(list.get(i).getDocument2());//EnterGoods的编号
                    enterGoodOk.setData1(good.getData1());
                    enterGoodOk.setForname(good.getForname());//供应商
                    enterGoodOk.setGoodname(list.get(i).getGoodname2());//商品名称
                    enterGoodOk.setNum(list.get(i).getNum());//数量
                    enterGoodOk.setMoney(list.get(i).getMoney());//金额
                    enterGoodOk.setStores(good.getStores());//门店
                    enterGoodOk.setRid(list.get(i).getRid());//仓库
                    add2 = enterGoodOkMapper.Add(enterGoodOk);
                }
            }
        }else if (ok.size()>0){
            add3 = enterGoodOkMapper.updateOkAndNo(eid);
        }


        /*操作:修改entergood的审核状态  添加IntoAndOut对象*/  //修改enterGoods的okandno2==0,没有退货
        //修改entergoods的okandno4=1,已经入库的意思    创建bank对象:入库后的消费
        int i2 = bankMapper.addBank(bank);
        int add=0;
        IntoAndOut intoAndOutBydocumentid = intoAndOutMapper.getIntoAndOutBydocumentid(good.getDocument());
        if(intoAndOutBydocumentid==null){
            // 添加IntoAndOut对象*
             add = intoAndOutMapper.Add(intoAndOut);
        }
        //修改entergood的审核状态
        int update = enterGoodMapper.update(eid);
        //修改enterGoods的okandno2==0,没有退货
        int i = enterGoodsMapper.updateOkandNo2(eid);

        //修改entergoods的okandno4=1,入库
        int i1 = enterGoodsMapper.updOkAndNo4(eid);

        if(add>0&&update>0&&add2>0&&i2>0 || add>0&&update>0&&add3>0&&i2>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public EnterGood getEnterGoodBYEid(Integer eid) {
        return enterGoodMapper.getEnterGoodBYEid(eid);
    }


    /*退货操作*/
    @Override
    public boolean outIn(Integer eid) {
        EnterGood good = enterGoodMapper.getEnterGoodByEGid(eid);
        //删除bank:入库的money
        int i4 = bankMapper.delBank(good.getGoodname());

        /*修改完成进货订单的okandno改为已经退货*/
        int i1 = enterGoodOkMapper.updateOkByOut(eid);

        /*删除已经入库的信息*/
        int i2 = intoAndOutMapper.deleteIn(eid);

        //修改okandno2:是否退货修改为退货(1)
        int i3 = enterGoodsMapper.updateByOkAndNo2(eid);

        if (i1 > 0 && i2 > 0&&i3>0 &&i4>0) {
            /*修改退货的时候的审核人*/
            int i = enterGoodMapper.updateOut(eid);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public EnterGood getEnterGoodByDocument2(String doument) {
        return enterGoodMapper.getEnterGoodByDocument2(doument);
    }




}
